package com.oauth2.demo.interceptor;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("执行了登录拦截器方法------------------请求url:{}", request.getRequestURL().toString());
//        try {
//            String remoteHost = request.getRemoteHost();
//            int remotePort = request.getRemotePort();
//            log.debug("客户端地址:{}:{}", remoteHost, remotePort);
//            String userName = null;
//            //统一拦截（查询当前session是否存在user）(这里user会在每次登陆成功后，写入session)
//            if (request.getSession() != null && ObjectUtil.isNotEmpty(request.getSession().getAttribute("accountNo"))) {
//                userName = (String) request.getSession().getAttribute("accountNo");
//                log.debug("--------------------用户名:{}", userName);
//                return true;
//            } else {
//                response.sendRedirect(request.getContextPath() + "/login");
//            }
//        } catch (IOException e) {
//            log.error("登录拦截器异常:{}", e);
//            e.printStackTrace();
//        }
        return true;
    }
}
